package com.hq.door.service.impl;

import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hq.door.dao.TClassDAO;
import com.hq.door.dao.TInstituteDAO;
import com.hq.door.dao.TMajorDAO;
import com.hq.door.dao.TRecordsDAO;
import com.hq.door.dao.TStudentDAO;
import com.hq.door.dao.door.IIdCustdeptDAO;
import com.hq.door.dao.door.IIdCustomersDAO;
import com.hq.door.pojo.TClass;
import com.hq.door.pojo.TMajor;
import com.hq.door.pojo.TRecords;
import com.hq.door.pojo.TStudent;
import com.hq.door.pojo.door.IdCustdept;
import com.hq.door.pojo.door.IdCustomers;
import com.hq.door.service.IUpdateDataService;
import com.hq.door.util.MD5Util;

@Service("updateDataService")
public class UpdateDataServiceImpl extends BaseServiceImpl<TMajor> implements
		IUpdateDataService {
	@Autowired
	TRecordsDAO tRecordsDAO;
	@Autowired
	TMajorDAO tMajorDAO;
	@Autowired
	TClassDAO tClassDAO;
	@Autowired
	TStudentDAO tStudentDAO;
	@Autowired
	TInstituteDAO tInstituteDAO;
	@Autowired
	IIdCustomersDAO idCustomersDAO;
	@Autowired
	IIdCustdeptDAO idCustdeptDAO;

	public IIdCustdeptDAO getIdCustdeptDAO() {
		return idCustdeptDAO;
	}

	public void setIdCustdeptDAO(IIdCustdeptDAO idCustdeptDAO) {
		this.idCustdeptDAO = idCustdeptDAO;
	}

	public IIdCustomersDAO getIdCustomersDAO() {
		return idCustomersDAO;
	}

	public void setIdCustomersDAO(IIdCustomersDAO idCustomersDAO) {
		this.idCustomersDAO = idCustomersDAO;
	}

	public TRecordsDAO gettRecordsDAO() {
		return tRecordsDAO;
	}

	public void settRecordsDAO(TRecordsDAO tRecordsDAO) {
		this.tRecordsDAO = tRecordsDAO;
	}

	public TMajorDAO gettMajorDAO() {
		return tMajorDAO;
	}

	public void settMajorDAO(TMajorDAO tMajorDAO) {
		this.tMajorDAO = tMajorDAO;
	}

	public TClassDAO gettClassDAO() {
		return tClassDAO;
	}

	public void settClassDAO(TClassDAO tClassDAO) {
		this.tClassDAO = tClassDAO;
	}

	public TInstituteDAO gettInstituteDAO() {
		return tInstituteDAO;
	}

	public void settInstituteDAO(TInstituteDAO tInstituteDAO) {
		this.tInstituteDAO = tInstituteDAO;
	}

	@Override
	public int updateStuData() {
		String sql = "SELECT * FROM ID_CUSTOMERS where CUSTDEPTID in(SELECT dp1.id FROM ID_CUSTDEPT dp1,ID_CUSTDEPT dp2 "
				+ "WHERE dp1.id=dp2.pid(+) and dp2.id is null and dp1.pid<>'58' and dp1.pid<>'0' and dp1.fulldpid not like '31,%') "
				+ "order by userid";

		List<IdCustomers> list = idCustomersDAO.querySQlList(sql);

		int saveCount = 0;
		for (IdCustomers idCustomer : list) {

			if (tStudentDAO.getCount(
					"select count(s.id) from TStudent s where s.code=?",
					idCustomer.getOutid()) == 0) {

				TStudent tStudent = new TStudent();
				tStudent.setCode(idCustomer.getOutid());
				tStudent.setName(idCustomer.getName());
				String cert = idCustomer.getCertificateno1();
				String paTemp = "";
				if(cert!=null||cert.length()>=6){
					paTemp = idCustomer.getOutid() + cert.substring(cert.length() - 6);
				}else{
					paTemp = idCustomer.getOutid() + "000000";
				}
				
				tStudent.setPassword(MD5Util.MD5(paTemp));
				IdCustdept idCustdept = idCustdeptDAO.get(idCustomer
						.getCustdeptid());
				TClass tClass = tClassDAO.queryOne(
						"select t from TClass t where t.name=?",
						idCustdept.getDeptname());
				if (tClass != null) {
					tStudent.setTClass(tClass);
					tStudentDAO.save(tStudent);
					saveCount++;
				}
			}
		}

		return saveCount;
	}

	@Override
	public int updateTeaData() {

		return 0;
	}

	@Override
	public int updateCardData() {

		return 0;
	}

	@Override
	public int updateRecordData() {

		return 0;
	}

	@Override
	public int updateMajorData() {
		StringBuffer hql = new StringBuffer(
				"SELECT dp1.* FROM ID_CUSTDEPT dp1,ID_CUSTDEPT dp2 WHERE dp1.id=dp2.pid(+) and dp2.id is null and dp1.pid<>'58' and dp1.pid<>'0' and dp1.fulldpid not like '31,%'");

		List<IdCustdept> list = idCustdeptDAO.querySQlList(hql.toString());

		int maxid = tMajorDAO.getCount("select max(tm.id) from TMajor tm");
		int saveCount = 0;

		for (IdCustdept idCustdept : list) {
			String name = idCustdept.getDeptname().substring(2, 4);
			if (tMajorDAO.getCount(
					"select count(tm.id) from TMajor tm where tm.name=?", name) == 0) {
				TMajor major = new TMajor();
				major.setName(name);
				maxid++;
				major.setCode(String.format("%06d", maxid));
				major.setTInstitute(tInstituteDAO.get(1));
				tMajorDAO.save(major);
				saveCount++;
			}
		}

		return saveCount;
	}

	@Override
	public int updateClassData() {
		StringBuffer hql = new StringBuffer(
				"SELECT dp1.* FROM ID_CUSTDEPT dp1,ID_CUSTDEPT dp2 WHERE dp1.id=dp2.pid(+) and dp2.id is null and dp1.pid<>'58' and dp1.pid<>'0' and dp1.fulldpid not like '31,%'");

		List<IdCustdept> list = idCustdeptDAO.querySQlList(hql.toString());

		int saveCount = 0;
		for (IdCustdept idCustdept : list) {
			TClass tClass = new TClass();
			if (tClassDAO.getCount(
					"select count(t.id) from TClass t where t.name=?",
					idCustdept.getDeptname()) == 0) {
				tClass.setName(idCustdept.getDeptname());
				tClass.setCode(idCustdept.getDpcode());
				TMajor tMajor = tMajorDAO.queryOne(
						"select t from TMajor t where t.name=?", idCustdept
								.getDeptname().substring(2, 4));
				if (tMajor != null) {
					tClass.setTMajor(tMajor);
					tClass.setGrade(idCustdept.getFullname().substring(0, 5));
					tClassDAO.save(tClass);
					saveCount++;
				} else {
					continue;
				}
			}
		}

		return saveCount;
	}

}
